<template>
    <div class="tags">
        <el-tag
            v-for="(item, index) in tags"
            :key="item.path"
            :closable = "item.name !== 'home'"
            :effect="$route.name === item.name ? 'dark' : 'plain'"
            @click="changeMenu(item)"
            @close="handleClose(item,index)"
            >      
            {{ item.label }}
        </el-tag>
    </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
    name: "CommonTag",
    data() {
        return {};
    },
    methods: {
        ...mapMutations(['closeTag']),
        // 点击tag跳转功能
        changeMenu(item) {
            console.log('item',item)
            this.$router.push({name: item.name})

        },
        //删除tag
        handleClose(item,index) {

            //调用 store中的 mutation
            this.closeTag(item)
            const length = this.tags.length
            //删除之后的跳转
            if(item.name !== this.$route.name){
                return
            }
            // 表示是删除的最后一项、
            if(index === length){
                //获取删除数据的前一条数据
                this.$router.push({
                    name: this.tags[index - 1].name
                })
            }else {
                 this.$router.push({
                    name: this.tags[index].name
                })
            }

            



            

        }
    },
    computed: {
        ...mapState({
            tags: (state) => state.tab.tabList,
        }),
    },
};
</script>

<style>
</style>